package com.cskaoyan.javase.util;

import java.io.*;

/**
 * 一个针对文件复制操作的工具类
 * 要想做文件复制,需要两个流对象:
 *      1.源文件的输入流对象
 *      2.目标文件的输出流对象
 *
 * @since 09:44
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class FileCopyUtils {
    // 私有化构造器,避免外界new对象
    private FileCopyUtils() {
    }

    // 字节流操作-单个字节操作完成文件复制功能
    public static void copySingleByte(InputStream in, OutputStream out) throws IOException {
        // 1.循环读,一边循环写
        int readData;
        while ((readData = in.read()) != -1) {
            out.write(readData);
        }
        // 2.关闭流
        in.close();
        out.close();
    }


    // 字节流操作-多个字节,使用byte数组操作完成文件复制功能
    public static void copyMultiByte(InputStream in, OutputStream out) throws IOException {
        // 1.循环读,一边循环写
        byte[] buf = new byte[1024];
        int readCount;
        while ((readCount = in.read(buf)) != -1) {
            out.write(buf, 0, readCount);
        }
        // 2.关闭流
        in.close();
        out.close();
    }

    // 用字符流复制文本文件,可以一个字符数组,一个字符数组的复制
    public static void copyMultiChar(Reader in, Writer out) throws IOException {
        // 1.循环读,一边循环写
        char[] buf = new char[1024];
        int readCount;
        while ((readCount = in.read(buf)) != -1) {
            out.write(buf, 0, readCount);
        }
        // 2.关闭流
        in.close();
        out.close();
    }

    // 用字符流复制文本文件,可以一个字符一个字符的复制
    public static void copySingleChar(Reader in, Writer out) throws IOException {
        // 1.循环读,一边循环写
        int readData;
        while ((readData = in.read()) != -1) {
            out.write(readData);
        }
        // 2.关闭流
        in.close();
        out.close();
    }


}
